package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappPageEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappPageMapper;
import com.infocollect.project.api.service.ITappPageService;
import com.infocollect.project.api.vo.PackageInterfaceInfoVO;
import com.infocollect.project.api.vo.SelectPageOrApiVO;
import com.infocollect.project.form.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TappPageServiceImpl extends ServiceImpl<TappPageMapper, TappPageEntity> implements ITappPageService {
    @Autowired
    private TokenService tokenService;
    /**
     * 通过ID查询单条数据
     *
     * @param fid 主键
     * @return 实例对象
     */
    @Override
    public TappPageEntity queryById(Long fid) {
        return baseMapper.selectById(fid);
    }

    /**
     * 修改数据
     *
     * @param tappPageEntity 实例对象
     * @return 实例对象
     */
    @Override
    public void update(TappPageEntity tappPageEntity) {
        baseMapper.updateById(tappPageEntity);
    }

    public R<String> addPage(String pageName, String title){
        QueryWrapper<TappPageEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("page_Name",pageName);
      TappPageEntity tappPageEntity=  baseMapper.selectOne(queryWrapper);
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        if(tappPageEntity!=null){
            tappPageEntity.setPageName(pageName);
            tappPageEntity.setDescription(title);
            tappPageEntity.setFlastupdatedate(new Date());
            tappPageEntity.setFlastupdateby(currentUserInfo.getUsername());
            baseMapper.updateById(tappPageEntity);
           return  R.newOk(tappPageEntity.getFid().toString());
        }else{
            tappPageEntity=new TappPageEntity();
            tappPageEntity.setFid(IdWorker.getId());
            tappPageEntity.setFcreateby(currentUserInfo.getUsername());
            tappPageEntity.setFcreationdate(new Date());
            tappPageEntity.setFlastupdateby(currentUserInfo.getUsername());
            tappPageEntity.setFlastupdatedate(new Date());
            tappPageEntity.setDescription(title);
            tappPageEntity.setPageName(pageName);
            tappPageEntity.setDeleted(false);
            baseMapper.insert(tappPageEntity);
            return  R.newOk(tappPageEntity.getFid().toString());
        }

    }
    /**
     * 新增数据
     *
     * @param tappPageEntity 实例对象
     * @return 实例对象
     */
    @Override
    public R<String> insert(TappPageEntity tappPageEntity) {
        QueryWrapper<TappPageEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("page_Name",tappPageEntity.getPageName());
         if(baseMapper.selectCount(queryWrapper)>0){
             return R.newError("输入的包名已经存在！");
         }
        baseMapper.insert(tappPageEntity);
         return R.newOk();
    }
    /**
     * 删除包
     * @param id
     */
    @Override
    public void deleteMapping(Long id){
        TappPageEntity tappPageEntity=new TappPageEntity();
        tappPageEntity.setFid(id);
        tappPageEntity.setDeleted(true);
        baseMapper.updateById(tappPageEntity);
    }

    /**
     * 获取包接口信息
     * @return
     */
    @Override
    public List<PackageInterfaceInfoVO> getPackageInterfaceInfo(){
       List<SelectPageOrApiVO> list=baseMapper.selectPageOrApi();
       return getChildResourceByPId(list,0L);
    }
    /**
     * 递归组装菜单树
     * @param menulist
     * @param pId
     * @return
     */
    List<PackageInterfaceInfoVO> getChildResourceByPId(List<SelectPageOrApiVO> menulist, Long pId){
        List<PackageInterfaceInfoVO> child = new ArrayList<>();
        menulist.forEach(ee->{
            if(ee.getParentId()!=null&&ee.getParentId().equals(pId)){
                PackageInterfaceInfoVO childitem=new PackageInterfaceInfoVO();
                BeanUtils.copyProperties(ee, childitem);
                childitem.setChildren(getChildResourceByPId(menulist,ee.getFid()));
                child.add(childitem);
            }
        });
        return child;
    }
}
